//
//  XUIButton.m
//  Etion
//
//  Created by  user on 11-3-14.
//  Copyright 2011 GuangZhouXuanWu. All rights reserved.
//

#import <CoreGraphics/CoreGraphics.h>

#import "XUIButton.h"

@implementation XUIButton

@synthesize szTag;

@synthesize buttonStyleType = _buttonStyleType;

@synthesize delegate = _delegate;

+ (XUIButton *)buttonWithStyleType:(XUIButtonStyleType)buttontype delegate:(id <XUIButtonDelegate>)delegate
{
    if (buttontype == XUIButtonStyleTypeNONE)
        return nil;
    NSInteger type = buttontype;
    if (XUIButtonStyleTypeLeftHeadImage == buttontype || XUIButtonStyleTypeTopHeadImage == buttontype || XUIButtonStyleTypeCenterImage == buttontype)
        buttontype = UIButtonTypeCustom;
    XUIButton *btn = (XUIButton *) [[self class] buttonWithType:buttontype];
    if ([btn isMemberOfClass:[XUIButton class]] == YES)
    {
        btn.delegate = delegate;
        btn.buttonStyleType = type;
        if([delegate respondsToSelector:@selector(clickedUpInsideOfButton:)])
            [btn addTarget:delegate action:@selector(clickedUpInsideOfButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    if (UIButtonTypeCustom == buttontype)
    {
        btn.backgroundColor = [UIColor clearColor];
        [btn SetTitleColor:[UIColor blackColor]];
    }
    if (XUIButtonStyleTypeTopHeadImage == type)
    {
        UIFont *font = nil;
        if ([btn.delegate respondsToSelector:@selector(titleFontInCustomButtonEx:)])
            font = [btn.delegate titleFontInCustomButtonEx:btn];
        [btn SetFont:font != nil? font : [UIFont systemFontOfSize:13]];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return btn;
}

+ (XUIButton *)buttonWithStyleType:(XUIButtonStyleType)buttontype
{
    return [XUIButton buttonWithStyleType:buttontype delegate:nil];
}

//- (CGRect)backgroundRectForBounds:(CGRect)bounds
//{
//	return bounds;
//}
//
//- (CGRect)contentRectForBounds:(CGRect)bounds
//{
//	return bounds;
//}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGRect frame = contentRect;

    if ([_delegate respondsToSelector:@selector(button:titleContentRect:)])
    {
        BOOL b = YES;
        if([_delegate respondsToSelector:@selector(handleTitleContentRectOfButton:)])
            b = [_delegate handleTitleContentRectOfButton:self];
        if(b == YES)
            return [_delegate button:self titleContentRect:frame];
    }

    switch ((NSInteger) _buttonStyleType)
    {
        case XUIButtonStyleTypeLeftHeadImage:
        {
            frame = [super titleRectForContentRect:contentRect];
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
            break;
        }
        case XUIButtonStyleTypeTopHeadImage:
        {
            UIFont *font = nil;
            if ([_delegate respondsToSelector:@selector(titleFontInCustomButtonEx:)])
                font = [_delegate titleFontInCustomButtonEx:self];
            
            CGSize strSize = [@"国" stringSizeWithFont:font != nil? font : [UIFont systemFontOfSize:13]];
            
            if([self GetImage] != nil)
            {
                CGSize imgSize = [self GetImage].size;
                
                CGFloat min = MIN(self.width, self.height - strSize.height);
                
                imgSize = [UIImage caleFitImageSize:imgSize targetSize:CGSizeMake(min, min)];
                
                CGFloat margin = (self.height - imgSize.height - strSize.height) / 3;
                
                frame = CGRectMake(0, margin + imgSize.height + margin, self.width, self.height - (margin + imgSize.height + margin) - margin);
            }
            else
                frame = CGRectMake(0, self.height - strSize.height, self.width, strSize.height);
            
            break;
        }
        default:
        {
            frame = [super titleRectForContentRect:contentRect];
            break;
        }
    }
    return frame;
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGRect frame = contentRect;

    if ([_delegate respondsToSelector:@selector(button:imageContentRect:)])
    {
        BOOL b = YES;
        if([_delegate respondsToSelector:@selector(handleImageContentRectOfButton:)])
            b = [_delegate handleImageContentRectOfButton:self];
        if(b == YES)
            return [_delegate button:self imageContentRect:frame];
    }

    switch ((NSInteger) _buttonStyleType)
    {
        case XUIButtonStyleTypeLeftHeadImage:
        {
            frame = [super imageRectForContentRect:contentRect];
            break;
        }
        case XUIButtonStyleTypeTopHeadImage:
        {
            UIFont *font = nil;
            if ([_delegate respondsToSelector:@selector(titleFontInCustomButtonEx:)])
                font = [_delegate titleFontInCustomButtonEx:self];
            
            CGSize strSize = [@"国" stringSizeWithFont:font != nil? font : [UIFont systemFontOfSize:13]];
            CGSize imgSize = [self GetImage].size;
            
            CGFloat min = MIN(self.width, self.height - strSize.height);
            
            imgSize = [UIImage caleFitImageSize:imgSize targetSize:CGSizeMake(min, min)];
            
            CGFloat margin = (self.height - imgSize.height - strSize.height) / 3;
            
            frame = CGRectMake((self.width - imgSize.width)/2, margin, imgSize.width, imgSize.height);
            
            break;
        }
        case XUIButtonStyleTypeCenterImage:
        {
            CGSize size = [self GetImage].size;
            if(size.width > self.width || size.height > self.height)
            {
                size = [UIImage imageNewSizeFromImageSize:size zoomToSize:self.size];
            }
            frame = CGRectMake((self.width - size.width)/2, (self.height-size.height)/2, size.width, size.height);
            break;
        }
        default:
        {
            frame = [super imageRectForContentRect:contentRect];
            break;
        }
    }
    return frame;
}

- (void)dealloc
{
    self.szTag = nil;
    superdealloc_mm;
}


@end
